package com.example.api;


import com.example.mapper.CategoryMapper;
import com.example.pojo.Category;
import com.example.service.CategoryService;


import com.example.util.Response;
import com.example.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@RequestMapping("category")
public class CategoryAPI {
    @Autowired
    private CategoryService categoryService;

//    @GetMapping("/searchcode")
//    public ResponseResult<Category> categorySearchByCode(String categoryCode)
//    {
//
//        Category categories = categoryService.selectByByCategoryCode(categoryCode);
//        if(categories!=null)
//            return new ResultVO(ResultCode.SUCCESS,categories);
//        else
//            return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
//    }


    @ResponseBody
    @GetMapping("/tb_category")
    public ResponseResult<List<Category>> selectCategoryList() {
        List<Category> categoryList = categoryService.getAllCategory();
        //注意：返回值为ResponseResult类型
        return Response.createOkResp(categoryList);
    }

    @GetMapping("/tb_category/{id}")
    public ResponseResult<Category> selectCategoryById(@PathVariable("id") int id) {
        Category category = categoryService.getCategoryById(id);
        return Response.createOkResp(category);
    }


    @PostMapping("/add")
    public ResponseResult<Category> addCategory(@RequestBody Category category) {
//        try {
//            categoryService.addCategory(category);
//            return Response.createOkResp("添加成功");
//        } catch (Exception e) {
//            return Response.createFailResp("添加失败");
//        }
        try {
            int num = categoryService.addCategory(category);
            System.out.println(num);
            return Response.createOkResp("添加成功");
        } catch (Exception e) {
            return Response.createFailResp("添加失败");
        }


//        Category category = new Category();
//        int num = CategoryService.addCategory(category);
//        System.out.println(category);
//        Category.setCategoryId(category.getCategory_id());
//        Category.setCategoryName(category.getCategory_name());
//        num += CategoryService.addCategory(category);
//        System.out.println(category);
//        System.out.println("影响行数:" + num);
//        return Response.createOkResp("添加成功");
    }


    @PostMapping("/delete")
    public ResponseResult<Category> categoryDelete(int category_id) {
        try {
            categoryService.deleteCategoryById(category_id);
            return Response.createOkResp("删除成功");
        } catch (Exception e) {
            return Response.createFailResp("删除失败");
        }
    }

    //添加验证
    @PostMapping("/update")
    public ResponseResult<Category> updateCategory(Category category) {
        try {
            categoryService.updateCategory(category);
            return Response.createOkResp("修改成功");
        } catch (Exception e) {
            return Response.createFailResp("修改失败");
        }
    }

//    添加分页
//    @GetMapping("/searchall")
//    public  ResultVO categorySearchAll(Integer page,Integer limit)
//    {
//        List<Category> categories = categoryService.selectAll(page==null?0:page,limit==null?10:limit);
//        if(categories.size()!=0)
//            return new ResultVO(ResultCode.SUCCESS,categories);
//        else
//            return new ResultVO(ResultCode.RECORD_NOT_FOUND,null);
//    }

}

